package so.sao.web.order;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import so.sao.core.PublicConstants;
import so.sao.core.ReturnMessage;
import so.sao.domain.TbOrder;
import so.sao.domain.TbOrderDetails;
import so.sao.service.order.OrderService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单管理.
 *
 * @author gxy on 2017/7/10.
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    Logger logger = Logger.getLogger(OrderController.class);

    @Autowired
    private OrderService orderService;

    /**
     * 分页获取所有订单.
     *
     * @return PageInfo<TbOrder>
     */
    @RequestMapping(value = "/orders", method = RequestMethod.GET)
    @ApiOperation(value = "获取所有订单", notes = "获取订单列表")
    public PageInfo<TbOrder> search(int pageNum) {
        List<TbOrder> orderList = orderService.getOrders(pageNum);
        return new PageInfo<>(orderList);
    }

    /**
     * 根据状态值分页获取所有订单.
     *
     * @param status
     * @return PageInfo<TbOrder>
     */
    @RequestMapping(value = "/orders/{status}", method = RequestMethod.GET)
    @ApiOperation(value = "根据状态值获取订单列表", notes = "根据status获取订单列表")
    public PageInfo<TbOrder> getByStatus(@PathVariable String status, int pageNum) {
        List<TbOrder> orderList = orderService.getOrderListByStatus(status, pageNum);
        return new PageInfo<>(orderList);
    }

    /**
     * 根据订单Id获取订单详情.
     *
     * @param orderId
     * @return List<TbOrderDetails>
     */
    @RequestMapping(value = "/orderDetails/{orderId}", method = RequestMethod.GET)
    @ApiOperation(value = "根据订单Id获取订单详情", notes = "根据orderId获取订单详情")
    public List<TbOrderDetails> getByOId(@PathVariable String orderId) {
        return orderService.getOrderDetailByOId(orderId);
    }

    /**
     * 更改某个订单的状态值 封装返回公共数据 并且错误打印日志.
     *
     * @param status
     * @param orderId
     * @return ReturnMessage 格式:{ "code": 0,"data": {} }
     * 成功:{
     * "code": 1,
     * "data": {
     * "msg": "SUCCESS"
     * }
     * }
     * 失败:{
     * "code": 0,
     * "data": {
     * "msg": "OrderController updateStatus error"
     * }
     * }
     */
    @RequestMapping(value = "orders/{orderId}/status", method = RequestMethod.POST)
    @ApiOperation(value = "更改某订单状态值", notes = "更改某订单status")
    public ReturnMessage updateStatus(String status, @PathVariable String orderId) {
        ReturnMessage msg = new ReturnMessage(PublicConstants.StatusCode.ERROR);
        try {
            orderService.updateOrderStatus(status, orderId);
            msg.setCode(PublicConstants.StatusCode.SUCCESS);
            msg.add(PublicConstants.StatusDetail.SUCCESS);
        } catch (Exception e) {
            logger.error(e);
            msg.add("OrderController updateStatus error");
            return msg;
        }
        return msg;
    }

    /**
     * POI导出Excel.
     *
     * @param request
     * @param response
     * @param status   状态值
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    @ApiOperation(value = "POI导出某状态下订单列表", notes = "POI导出某状态下订单列表")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response, String status) {
        orderService.exportExcel(request, response, status);
    }
}
